package com.bawei.demo_mvvm

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.bawei.demo_mvvm.state.VideoState
import com.bawei.demo_mvvm.viewmodel.VideoViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

    private val viewModel:VideoViewModel by viewModels() //懒加载，用的时候才会创建实例，不用不创建
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        //方案一:livedata 简单数据 自动感知生命周期
        viewModel.video() //发起网络请求
        viewModel.videoLiveData.observe(this){//自动感知生命周期，页面活跃的时候才订阅数据
            Toast.makeText(this,"数据"+it.size,Toast.LENGTH_SHORT).show()
        }

        //方案二:flow 复杂数据 手动感知生命周期repeatOnLifecycle
        viewModel.video2()
//        lifecycleScope.launch {
//            repeatOnLifecycle(Lifecycle.State.RESUMED){//感知生命周期，页面RESUMED才订阅数据库
//                viewModel.videoShareFlow.collect{ it->
//                    Toast.makeText(this@MainActivity,"数据"+it.size,Toast.LENGTH_SHORT).show()
//                }
//            }
//
//        }
        viewModel.videoShareFlow.observeWithLifecycle(this){
            Toast.makeText(this@MainActivity,"数据"+it.size,Toast.LENGTH_SHORT).show()
        }
        //方案三:stateFlow 状态
        viewModel.video3()
//        lifecycleScope.launch {
//            repeatOnLifecycle(Lifecycle.State.RESUMED){//感知生命周期，页面RESUMED才订阅数据库
//                viewModel.videoStateFlow.collect{
//                    when(it){
//                        is VideoState.Success->{
//                            Toast.makeText(this@MainActivity,"数据"+it.data.size,Toast.LENGTH_SHORT).show()
//                        }
//                        is VideoState.Error->{
//                            Toast.makeText(this@MainActivity,"错误"+it,Toast.LENGTH_SHORT).show()
//                        }
//                        is VideoState.Loading->{
//                            Toast.makeText(this@MainActivity,"加载中",Toast.LENGTH_SHORT).show()
//                        }
//
//                    }
//                }
//            }
//
//        }
        viewModel.videoStateFlow.observeWithLifecycle(this){
            when(it){
                is VideoState.Success->{
                    Toast.makeText(this@MainActivity,"数据"+it.data.size,Toast.LENGTH_SHORT).show()
                }
                is VideoState.Error->{
                    Log.d("TAG", "onCreate: "+it)
                    Toast.makeText(this@MainActivity,"错误"+it,Toast.LENGTH_SHORT).show()
                }
                is VideoState.Loading->{
                    Toast.makeText(this@MainActivity,"加载中",Toast.LENGTH_SHORT).show()
                }

            }
        }
    }
}